set more off 

use "ssi", clear

local control  income male married family_size age hs coll grad white black i.region

xtile q = predict_obama, nq(5)
gen x = 1 if predict_obama > 50
replace x = 0 if predict_obama <= 50
replace x = . if mi(predict_obama)
 

su predict_obama, det
keep if predict_obama > 0 & predict_obama < 100

replace predict_obama = (predict_obama - r(min)) / (r(max) - r(min))

gen republicanXpredict_obama = republican * predict_obama
gen independentXpredict_obama = independent * predict_obama

label variable republicanXpredict_obama "Republican X prediction"
label variable independentXpredict_obama "Independent X prediction"

eststo clear

gen laggeddv = .
foreach y in change_eco ln_vacation ln_totgiv {
	replace laggeddv = l`y'
	eststo: reg d`y' republican predict_obama republicanXpredict_obama  `control', r
	eststo: reg d`y' republican predict_obama republicanXpredict_obama   laggeddv `control', r	
}

label variable laggeddv "Lagged outcome"

#delimit;

esttab
	using "tableA4.tex"
	,
		order(republican predict_obama republicanXpredict_obama)	
		replace
 		gap
		compress
		stats(N r2, fmt(%20.00fc %20.02fc) label("Sample size" "R-squared"))
		booktabs
		cells(b(fmt(2) star) se(par))
		label
 		drop(*region*)
		collabels(none)
		width(\textwidth)
		mgroups(
			"Perceptions" "Vacation" "Giving"
			,
				pattern(1 0 1 0 1 0)
				prefix(\multicolumn{@span}{c}{) suffix(})
				span erepeat(\cmidrule(lr){@span})
		)	
		nomtitles
		star("+" .1 "*" .05 "**" .01 "***" .001)
		;
	
#delimit cr
 
